package generator.statements;

import generator.collections.ReservationTable;
import java.sql.Date;

/**
 * Reservation SQL record mapper.
 * 
 * @author Necro
 * @author Marek Snopkowski <snpy@snpy.info>
 * @version 12.04.2008
 */
public class Reservation extends Statement
{    
    private int reservationID;
    
    private Offer offer;
    private Customer customer;
    private PaymentForm paymentForm;
    
    private float to_pay;
    private float paid; 
    private Date reservationDate;
    private int expiresAfter;
    private float discount;
    private int cancelled;

    //@todo funkcjonalność
    public Reservation(Offer offer, Customer customer, PaymentForm paymentForm,
            float paid, Date reservationDate, int expiresAfter, 
            float discount, int cancelled)
    {
        this.reservationID   = ReservationTable.autoincrement();
        this.offer           = offer;
        this.customer        = customer;
        this.paymentForm     = paymentForm;
        this.paid            = setPaid(paid);
        this.reservationDate = reservationDate;
        this.expiresAfter    = expiresAfter;
        this.discount        = discount;
        this.cancelled       = cancelled;
        
        this.to_pay          = setToPay();
    }
    
    private float setToPay()
    {
        float toPay = offer.getPrice() - discount - paid;
        return toPay < 0 ? 0 : toPay;
    }
    
    public void multiplyCost(int n){
        this.to_pay*=n;
    }
    private float setPaid(float paid)
    {
        return (offer.getPrice() > paid) 
                ? paid 
                : offer.getPrice();
    }

    public void charge(float charge) {
        this.paid=setPaid(this.paid+charge);
        to_pay=setToPay();
    }
    
    public Date getReservationDate()
    {
        return reservationDate;
    }
    
    @Override
    public int getId()
    {
        return reservationID;
    }
    
    public void increasePrice(float diff)
    {
        this.to_pay+=diff;
    }
    
    public Object[] getFieldsValueCollection()
    {
        return new Object[]{ reservationID, offer.getId(), customer.getId(), paymentForm.getId(), 
                to_pay, paid, reservationDate, expiresAfter, discount, cancelled };
    }

    public float getToPay() {
        return to_pay;
    }

    public int getCancelled() {
        return cancelled;
    }

    
    
    public Offer getOffer() {
        return offer;
    }
    
    public String getFieldsNameSet()
    {
        return "[reservation_id], [offer_id], [customer_id], [payment_form_id], " +
                "[to_pay], [paid], [reservation_date], [expires_after], " +
                "[discount], [cancelled]";
    }

    @Override
    public String getTableName()
    {
        return "reservation";
    }
}
